package PersistanceLayer;

import BusinessLayer.Annee;
import BusinessLayer.Semestre;

import PersistanceLayer.DAO;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

import PersistanceLayer.DAO;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

import java.util.ArrayList;

import jdbc.ConnectionToOracle;

public class SemestreDAO extends DAO<Semestre> {

     private static final SemestreDAO instance = new SemestreDAO();

     /* Methode permettant de recuperer l'objet unique de type SemestreDAO
     */
    public final static SemestreDAO getInstance() {
        return instance;
    }

    @Override
    public boolean create(Semestre obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Semestre obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean update(Semestre ancien, Semestre nouveau) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Semestre find(Semestre obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public boolean delete(Semestre obj) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public ArrayList<Semestre> getListe() {
    	String codeSemestre;
    	String libelleSemestre;
    	String nbrUeFacultatives;
    	
    	ArrayList<Semestre> ensSem = new ArrayList<Semestre>();

    	
    	try {
    		
    		java.sql.Statement state =this.connect.createStatement();
    		
			ResultSet res = this.connect.createStatement()
			.executeQuery("SELECT * FROM SEMESTRE");
		
			
			while(res.next()){	
				if(res.getObject(1) != null)
					codeSemestre = res.getObject(1).toString();
				else
					codeSemestre = "";
				
				if(res.getObject(3) != null)
					libelleSemestre = res.getObject(3).toString();
				else
					libelleSemestre = "";
				
				if(res.getObject(4) != null)
					nbrUeFacultatives = res.getObject(4).toString();
				else
					nbrUeFacultatives = "";
				
				ensSem.add(new Semestre(codeSemestre,libelleSemestre,nbrUeFacultatives));
				
			}
			
			state.close();
			res.close();
			
			return ensSem;
    	} catch (SQLException e) {
			// 
    		
			e.printStackTrace();
			return null;
		}
    	
    }
    

	@Override
	public ArrayList<Semestre> getListe(String name) {
		// TODO Auto-generated method stub
		ArrayList<Semestre> ensSemestres = new ArrayList<Semestre>();
		
		try {
			
			//Cr�ation d'un objet Statement
			java.sql.Statement state = this.connect.createStatement();
			//L'objet ResultSet contient le r�sultat de la requ�te SQL
			ResultSet result = state.executeQuery("SELECT * " +
					"FROM SEMESTRE " +
					"WHERE VERSIONETAPE = '"+name+"'");
			
			//On r�cup�re les MetaData

			while(result.next()){		
				ensSemestres.add(new Semestre(result.getObject(1).toString()));

			}


             result.close();
             state.close();

			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		return ensSemestres;
	}

	@Override
	public String getNom(String name) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean update(String a, String b, String c, String d) {
		// TODO Auto-generated method stub
		return false;
	}


    
  /*  public static void main(String[] args){
		
    	Connection connexion;
    	SemestreDAO semestre = new SemestreDAO();
    	ArrayList<Semestre> sem = new ArrayList<Semestre>();
    	
    	connexion = ConnectionToOracle.getInstance();
    	sem=semestre.getListe("4ig");
    	System.out.println(sem.toString());
    	}
    	*/


}